package com.kobe.game_20;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * 
 * In the 20��20 grid below, four numbers along a diagonal line have been marked
 * in red.
 * 
 * The diagram is shown in the direction of source!!
 * 
 * 
 * The product of these numbers is 26 �� 63 �� 78 �� 14 = 1788696.
 * 
 * What is the greatest product of four numbers in any direction (up, down,
 * left, right, or diagonally) in the 20��20 grid?
 * 
 * the Result is: 70600674
 * 
 */
public class Game11 {
    /**
     * int[y][x]
     */

    public static void main(String[] args) throws IOException {
        int[][] array = getArray();
        System.out.println("the Result is:  " + getMaxResult(array));
    }

    private static int[][] getArray() throws IOException {
        int y = 0;
        int[][] array = new int[20][20];
        File file = new File("src/source/Game11_Diagram.txt");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = reader.readLine()) != null) {
            String[] lineArray = line.split(" ");
            for (int x = 0; x < 20; x++) {
                array[y][x] = Integer.valueOf(lineArray[x]);
            }
            y++;
        }
        return array;
    }

    private static int getMaxResult(int[][] array) {
        int x_incre = 0;
        int y_incre = 0;
        int result = 0;
        int tempResult = 0;

        // ֻ���� �ҡ��¡����ϡ����¡��������з���
        // i: x�ᶨλ
        for (int i = 0; i < 20; i++) {
            // m: y�ᶨλ
            for (int m = 0; m < 20; m++) {

                // ����
                if (m >= 3 && i <= 16) {
                    x_incre = 1;
                    y_incre = -1;
                    tempResult = getProduct(array, i, m, x_incre, y_incre);
                    if (tempResult > result) {
                        result = tempResult;
                    }
                }

                // ����
                if (m <= 16 && i <= 16) {
                    x_incre = 1;
                    y_incre = 1;
                    tempResult = getProduct(array, i, m, x_incre, y_incre);
                    if (tempResult > result) {
                        result = tempResult;
                    }
                }

                // ��
                if (m <= 16) {
                    x_incre = 0;
                    y_incre = 1;
                    tempResult = getProduct(array, i, m, x_incre, y_incre);
                    if (tempResult > result) {
                        result = tempResult;
                    }
                }

                // ��
                if (i <= 16) {
                    x_incre = 1;
                    y_incre = 0;
                    tempResult = getProduct(array, i, m, x_incre, y_incre);
                    if (tempResult > result) {
                        result = tempResult;
                    }
                }
            }
        }

        return result;
    }

    private static int getProduct(int[][] array, int x, int y, int x_incre,
            int y_incre) {
        int result = 1;
        for (int i = 0; i < 4; i++) {
            result = result * array[y + y_incre * i][x + x_incre * i];
        }
        return result;
    }
}
